package huriezy.quickcontacts.activities.fragments;

import java.io.File;

import huriezy.quickcontacts.R;
import huriezy.quickcontacts.activities.UserSelectionFragmentActivity;
import huriezy.quickcontacts.models.ContactController;
import huriezy.quickcontacts.models.LogDebug;
import huriezy.quickcontacts.models.interfaces.IContactManager;
import huriezy.quickcontacts.models.interfaces.IPictureManager;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SocialNetworkFragment extends ContactFragment {

	public static final int	SOCIAL_REQUEST_CODE	= 4569;

	public static SocialNetworkFragment newInstance() {
		SocialNetworkFragment instance = new SocialNetworkFragment();
		return instance;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View result = inflater.inflate(R.layout.fragment_social_network, container, false);

		initActions(result);
		return result;
	}

	private void initActions(View view) {
		Button facebookButton = (Button) view.findViewById(R.id.facebook_authButton);
		final EditText contactNameEditText = (EditText) view.findViewById(R.id.social_name_editText);

		facebookButton.setText("Search in Facebook");
		
		// Retrieve contact name if possible
		IContactManager contactManager = ContactController.getInstance().getContactManager(getActivity());
		String contactName = contactManager.getContactData(IContactManager.NAME_TAG);

		// If contact name not null, we set the value to this fragment edit text
		if (contactNameEditText != null && contactName != null) {
			contactNameEditText.setText(contactName);
		}

		if (facebookButton != null) {
			facebookButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (contactNameEditText != null) {
						String contactName = contactNameEditText.getText().toString();
						if(contactName != null && contactName.length() > 4){
							startUserSelectionActivity(contactName);
						}
						else{
							String invalidText = getActivity().getResources().getString(R.string.invalid_profile_name_text);
							Toast.makeText(getActivity(), invalidText, Toast.LENGTH_SHORT).show();
						}
					}
				}
			});
		}
	}

	private void startUserSelectionActivity(String contactName) {
		Intent intent = new Intent(getActivity(), UserSelectionFragmentActivity.class);
		intent.putExtra(UserSelectionFragmentActivity.USED_SOCIAL_KEY,
				UserSelectionFragmentActivity.FACEBOOK_SOCIAL_TYPE);
		intent.putExtra(FacebookPickerFragment.SEARCH_NAME_DATA, contactName);
		startActivityForResult(intent, SOCIAL_REQUEST_CODE);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK && data != null) {
			if (requestCode == SOCIAL_REQUEST_CODE) {
				Bundle userResult = data.getBundleExtra(UserSelectionFragmentActivity.SELECTED_USER);
				if (userResult != null) {
					LogDebug.info("Result of Facebook selection activity : " + userResult, getClass());
					IContactManager contactManager = ContactController.getInstance().getContactManager(getActivity());

					String name = userResult.getString(FacebookPickerFragment.NAME_FIELD);
					String email = userResult.getString(FacebookPickerFragment.EMAIL_FIELD);
					String address = userResult.getString(FacebookPickerFragment.ADDRESS_FIELD);
					Bitmap pictureBitmap = userResult.getParcelable(FacebookPickerFragment.PICTURE_BITMAP_FIELD);

					if (name != null) {
						contactManager.putContactData(IContactManager.NAME_TAG, name);
					}
					if (email != null) {
						contactManager.putContactData(IContactManager.EMAIL_TAG, email);
					}
					if (address != null) {
						contactManager.putContactData(IContactManager.ADDRESS_TAG, address);
					}
					if (pictureBitmap != null) {
						IPictureManager pictureManager = ContactController.getInstance().getPictureManager(
								getActivity());
						if (pictureManager != null) {
							File tempImgFile = pictureManager.saveTempImageFile(pictureBitmap);
							String path = tempImgFile.getAbsolutePath();
							contactManager.putContactData(IContactManager.PICTURE_TAG, path);
						}
					}
				}
				else {
					LogDebug.warn("Result from user selection activity is null", getActivity());
				}
			}
		}
	}

	@Override
	public void saveContactData() {
		// Nothing to save
	}

	@Override
	public void clearContactData() {
		// Nothing to clear
	}
}
